﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.Data;

namespace elr_tracking_bal
{
    public class APP_ROL
    {
        public int ID_ROL;
        public string ROL_NAME;
        public int ROL_PRIORITY;
        public bool check;
        /// <summary>
        /// Método para insertar un Rol
        /// </summary>
        /// <param name="rol"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg InsertRol(APP_ROL rol)
        {
            APP_ROL inser;
            elr_tracking_bal.UpsertStateMsg inserRol;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_APP_INSERT_ROL " + "'" + rol.ROL_NAME + "'," + rol.ROL_PRIORITY + "");
                inser = new APP_ROL(rol.ROL_NAME, rol.ROL_PRIORITY);
                inserRol = new elr_tracking_bal.UpsertStateMsg("Registros guardados " + dt.ToString(), true);
                return inserRol;
            }
            catch (Exception ex) {
                inserRol = new elr_tracking_bal.UpsertStateMsg("Error al guardar " + ex, false);
                return inserRol;
            }
        }
        /// <summary>
        /// Método para eliminar Roles
        /// </summary>
        /// <param name="ID_ROL"></param>
        /// <returns></returns>
        public static elr_tracking_bal.DeleteStateMsg DeleteRol(string[] ID_ROL)
        {
            elr_tracking_bal.DeleteStateMsg delete;
            string join;
            if (ID_ROL == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(",", ID_ROL);
            }
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_APP_DELETE_ROL '" + join + "'");
                delete = new elr_tracking_bal.DeleteStateMsg("Registro eliminado " + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new elr_tracking_bal.DeleteStateMsg("Error al eliminar " + ex, false);
                return delete;
            }
        }
        /// <summary>
        /// Método para modificar un Rol
        /// </summary>
        /// <param name="rol"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg UpdateRol(APP_ROL rol)
        {
            APP_ROL upsert;
            elr_tracking_bal.UpsertStateMsg upsertRol;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_APP_UPDATE_ROL " + rol.ID_ROL + ",'" + rol.ROL_NAME +
                    "','" + rol.ROL_PRIORITY + "'");

                upsert = new APP_ROL(rol.ID_ROL, rol.ROL_NAME, rol.ROL_PRIORITY);
                upsertRol = new elr_tracking_bal.UpsertStateMsg("Registros actualizados " + dt.ToString(), true);
                return upsertRol;
            }
            catch (Exception ex)
            {
                upsertRol = new elr_tracking_bal.UpsertStateMsg("Error al actualizar " + ex, false);
                return upsertRol;
            }
        }
        /// <summary>
        /// Método para obtener un Rol
        /// </summary>
        /// <param name="ID_ROL"></param>
        /// <returns></returns>
        public static APP_ROL GetOneRol(int ID_ROL)
        {
            try
            {
                APP_ROL rol;
                DataTable dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_APP_GETONE_ROL " + ID_ROL + "");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (ID_ROL == 0)
                {
                    return null;
                }
                else
                {
                    rol = new APP_ROL(Convert.ToInt32(dt.Rows[0]["ID_ROL"]), Convert.ToString(dt.Rows[0]["ROL_NAME"]), Convert.ToInt32(dt.Rows[0]["ROL_PRIORITY"]));
                }
                return rol;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Método para obtener la lista de Roles
        /// </summary>
        /// <param name="ID_ROL"></param>
        /// <returns></returns>
        public static List<APP_ROL> GetDataListRol(string[] ID_ROL)
        {
            string join;
            if (ID_ROL == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(",", ID_ROL);
            }
            try
            {
                DataTable dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_APP_GETLIST_ROL");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<APP_ROL> list = dt.AsEnumerable().Select(
                        row => new elr_tracking_bal.APP_ROL
                        {
                            ID_ROL = row.Field<int?>(0).GetValueOrDefault(),
                            ROL_NAME = String.IsNullOrEmpty(row.Field<string>(1)) ? "" : row.Field<string>(1),
                            ROL_PRIORITY = row.Field<int?>(2).GetValueOrDefault(),
                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Constructor sin parámetros
        /// </summary>
        public APP_ROL() {}
        /// <summary>
        /// Constructor con un parámetro
        /// </summary>
        /// <param name="ID_ROL"></param>
        public APP_ROL(int ID_ROL)
        {
            this.ID_ROL = ID_ROL;
        }
        /// <summary>
        /// Constructor con dos parámetros
        /// </summary>
        /// <param name="ROL_NAME"></param>
        /// <param name="ROL_PRIORITY"></param>
        public APP_ROL(string ROL_NAME, int ROL_PRIORITY)
        {
            this.ROL_NAME = ROL_NAME;
            this.ROL_PRIORITY = ROL_PRIORITY;
            this.check = false;
        }
        /// <summary>
        /// Constructor con tres parámetros
        /// </summary>
        /// <param name="ID_ROL"></param>
        /// <param name="ROL_NAME"></param>
        /// <param name="ROL_PRIORITY"></param>
        public APP_ROL(int ID_ROL, string ROL_NAME, int ROL_PRIORITY)
        {
            this.ID_ROL = ID_ROL;
            this.ROL_NAME = ROL_NAME;
            this.ROL_PRIORITY = ROL_PRIORITY;
            this.check = false;
        }
    }
}